import { BrowserWindow, ipcMain } from "electron"
import { getWindow } from "./windowProxy";
import { initWs } from "./wsClient";
import store from "./store";


const onLoginOrRegister = ()=>{


  ipcMain.handle("loginOrRegister",(e,isLogin)=>{
     const login_width = 375;
     const login_height = 365;
     const register_height = 485;

     const mainWindow = getWindow("main");
     mainWindow.setResizable(true);
     mainWindow.setMinimumSize(login_width,login_height);

     if(isLogin){
          mainWindow.setSize(login_width,login_height);
     }else{
        mainWindow.setSize(login_width,register_height);
     }

    //是否可以改变大小
     mainWindow.setResizable(false);

  })

}


const onWinTitleOp=()=>{
  ipcMain.on("winTitleOp",(e,{action,data})=>{
      const webContents = e.sender;
      const win = BrowserWindow.fromWebContents(webContents);
      switch(action){
        case "close":{
          if(data.closeType ==0){
             win.forceClose = data.forceClose;
             win.close();
          }else{
            win.setSkipTaskbar(true);
            win.hide();
          }
          break;
        }
        case "minimize":{
          win.minimize();
          break;
        }
        case "maximize":{
          win.maximize();
          break;
        }
        case "unmaximize":{
            win.unmaximize();
            break;
        }

      }

  })

}

// 监听
const onLoginSuccess =()=>{
    ipcMain.handle("loginSuccess",(e,{userInfo,wsUrl})=>{
          const mainWindow = getWindow("main");
          //调节窗口大小
          mainWindow.setResizable(true);
          mainWindow.setMinimumSize(720,480);
          mainWindow.setSize(720,480);
          //是否可以改变大小
          mainWindow.setResizable(false);
          console.log("userInfo",userInfo);

          store.initUserId(userInfo.userId);
          store.setData("userInfo",userInfo);
          //建立WS连接
          initWs(wsUrl + userInfo.token)


    })
}

export {
  onLoginOrRegister,onWinTitleOp,onLoginSuccess
}
